
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM4;

public class MyAsm {
    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
//        File file = new File("./out/production/asm/Stutem.class");
//        file.delete();
//        new Stutem();
        ClassReader cr = new ClassReader("Stutem");
        ClassWriter cw = new ClassWriter(0);
        ClassVisitor cv = new ClassVisitor(ASM4,cw) {
            @Override
            public void visitEnd() {
                //添加一个成员
                cw.visitField(ACC_PUBLIC,"name","Ljava/lang/String;", null ,null);
                super.visitEnd();
            }
        };

        cr.accept(cv,0);
        byte[] b = cw.toByteArray();
        MyClassLoader my = new MyClassLoader();
        Class cls = my.defineClass("Stutem",b);


        Stutem cs = (Stutem) cls.newInstance();
        System.out.println(cls.getFields()[0].getName());
//        更改class文件
        FileOutputStream out = new FileOutputStream("./out/production/asm/Stutem.class");
        out.write(b);
        out.flush();
        out.close();
        //class已经被重新生成了
        Stutem s = new Stutem();
        s.name = "asd";
//        System.out.println(s.name);



    }

}
